<?php
/**
 * User: gli
 * Date: 13-3-11
 * Time: 8:59 PM
 */
class Experiment extends Base {

	protected function init() {
		parent::init();

		$this->attributes['pid'] = 0;
		$this->attributes['name'] = '';
		$this->attributes['url'] = '';
	}

	public function __construct($id=null) {
		$this->init();

		if (!empty($id)) {
			if ($id instanceof stdClass) {
				if (!$this->load($id->id)) {
					throw new Exception("Load experiment by given id: {$id->id}");
				}
			} else if ($id instanceof Project) {
				foreach ($id->attributes as $key => $value) {
					$this->attributes[$key] = $value;
				}
			} else if (is_string($id) || is_numeric($id)) {
				if (!$this->load($id)) {
					throw new Exception("Fail to load project by given id: $id");
				}
			} else {
				throw new InvalidParameterException("Invalid parameter to load experiment by given id: {$id}");
			}
		}
	}

	protected function load($id) {
		$id = (int)$id;
		if (!$id) return false;

		$row = get_experiment_object_as_row($id);
		if ($row) {
			$obj_array = (array) $row;
			foreach ($obj_array as $key => $value) {
				$this->attributes[$key] = $value;
			}
			return true;
		}

		return false;
	}

	protected function  save() {
		return create_experiment(
			$this->get('id'),
			$this->get('pid'),
			$this->get('name'),
			$this->get('url')
		);
	}
}

/**
 * Create or update the experiment
 * @param int $id experiment id
 * @param int $pid project id
 * @param string $name experiment name
 * @param string $url experiment url
 *
 * @return bool|int
 */
function create_experiment($id, $pid, $name, $url) {
	$id = (int) $id;
	$pid = (int) $pid;
	$name = mysql_real_escape_string($name);
	$url = mysql_real_escape_string($url);

	$row = null;
	if (!empty($id)) {
		$row = get_experiment_object_as_row($id);
	}

	$time = time();
	if ($row) {
		// update
		$query = "UPDATE experiments SET pid={$pid}, name='{$name}', url='{$url}', updated={$time}";
		$result = update_data($query);
		if ($result != false) {
			return $id;
		}
	} else {
		if (!$pid && !empty($name) && !empty($url)) {
			$query = "INSERT INTO experiments(pid, name, url, created, updated) VALUES($pid, '$name', '$url', $time, $time)";
			$id = insert_data($query);
			if ($id != false) {
				return $id;
			}
		}
	}

	return false;
}

/**
 * Get experiment object by given id
 * @param int $id experiment id
 *
 * @return bool|object
 */
function get_experiment_object_as_row($id) {
	$id = (int) $id;
	if (!$id) return false;

	$query = "SELECT * FROM experiment WHERE $id={$id} LIMIT 1";
	return get_data_row($query);
}

/**
 * Get user's experiment by given name
 * @param $name experiment name
 * @param $uid user id
 * @param $pid project id
 *
 * @return array
 */
function get_experiment_object_by_name($name, $uid, $pid) {
	$name = mysql_real_escape_string($name);
	$uid = (int)$uid;
	$pid = (int)$pid;
	if (empty($name) || !$uid || !$pid) return array();

	$result = array();
	$query = "SELECT * FROM experiment WHERE uid={$uid} AND pid={$pid} AND name='{$name}'";
	try {
		$rows = get_data($query);
		if ($rows) {
			foreach ($rows as $row) {
				$result[] = new Experiment($row);
			}
		}
	} catch (DataBaseException $ex) {
		error_log("DataBaseException in get_experiment_object_by_name() function");
	}

	return $result;
}

/**
 * Get user's all experiments of the project
 * @param int $uid user id
 * @param int $pid project id
 * @return array
 */
function get_user_experiments($uid, $pid) {
	$uid = (int)$uid;
	$pid = (int)$pid;
	if (!$uid || !$pid || $uid < 0 || $pid < 0) return array();

	$result = array();
	$query = "SELECT * FROM experiment WHERE uid={$uid} AND pid={$pid}";
	try {
		$rows = get_data($query);
		if ($rows) {
			foreach ($rows as $row) {
				$result[] = new Experiment($row);
			}
		}
	} catch (DataBaseException $ex) {
		error_log("DataBaseException in get_experiment_object_by_name() function");
	}

	return $result;
}